package com.tulele.vtool.interceptor;

import com.tulele.vtool.exception.ServiceException;
import com.tulele.vtool.model.UserGroupOpenId;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.IUserInfoService;
import com.tulele.vtool.service.UserGroupGzhService;
import com.tulele.vtool.utils.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Slf4j
@Component
public class UserReqInterceptor implements HandlerInterceptor {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private UserGroupGzhService userGroupGzhService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestURI = request.getRequestURI();

        String userId = request.getHeader("userId");

        String userSessionId = request.getHeader("userSessionId");

        log.info("[requestURI] = " + requestURI + " [userId] = " + userId + " [userSessionId] = " + userSessionId);

        if (!requestURI.contains("/druid") && !requestURI.contains("/wechat") && !requestURI.contains("/cache")
                && !requestURI.contains(".txt")//20200205 hyl 放行static 中的 微信授权文件
                && !requestURI.contains("/myTest")
                && !requestURI.contains("vtool_versions.html")//20210220 hyl 版本文件放行
                && !requestURI.contains("/getSysRealmLink")//20210220 hyl 版本文件放行
                && !requestURI.contains("/selectCacheLinkByGzhCode")//20210220 hyl 版本文件放行
                ) {

            if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId.trim())) {

                throw new ServiceException("用户未登录", ReturnCode.NOT_LOGIN);

            }

            String openId = null;
            if (StringUtils.isNotBlank(userId) && StringUtils.isNumeric(userId)) {

                openId = userInfoService.getUserOpenId(Integer.parseInt(userId));
                boolean verifyResult = ToolUtil.verify(userId + openId, userSessionId);

                //region （公众号做通）20210902
                if(!verifyResult){
                    List<UserGroupOpenId> openIds= userGroupGzhService.getUserGroupOpenIds(Integer.parseInt(userId));
                    if(openIds!=null && openIds.size()>0){
                        for(UserGroupOpenId oId:openIds){
                            if(ToolUtil.verify(userId + oId.getOpenId(), userSessionId)){
                                verifyResult=true;
                                break;
                            }
                        }
                    }
                }
                //endregion

                if (StringUtils.isBlank(openId) || StringUtils.isBlank(userSessionId)
                        || StringUtils.isBlank(openId) || !verifyResult) {

                    throw new ServiceException("用户未登录", ReturnCode.NOT_LOGIN);  //调试时屏蔽

                }
            }


        }

        return true;
    }

}
